#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define str string
#define all(x) (x).begin(),(x).end()
#define ms(a,t) memset(a,t,sizeof(a))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define db double
const int maxn = 5e6+10;
const int mod = 998244353;
const ll INF = 0x3f3f3f3f;
const db eps = 1e-7;
ll l,r;
ll ans = 0;
ll pw[40];
ll f[40][2][2];
ll dfs(int p,bool d,bool u) {
//	cerr<<p<<' '<<d<<' '<<u<<'\n';
	if(!u && !d) {
		return pw[p+1];
	}
	if(p == -1) {
		return 1;
	}
	if(f[p][d][u]) return f[p][d][u];
	int L = (d ? ((l>>p)&1) : 0);
	int R = (u ? ((r>>p)&1) : 1);
	for(int x = L; x <= 1; x++) {
		for(int y = x; y <= R; y++) {
			f[p][d][u] += dfs(p-1,d&(x==L),u&(y==R));
		}
	}
	return f[p][d][u];
}
void Solve() {
	ms(f,0);
	cin >> l >> r;
	ans = 0;
	ans = dfs(30,1,1);
	cout<<ans<<'\n';
}
int main() {
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	pw[0] = 1; for(int i = 1; i <= 30; i++) pw[i] = pw[i-1] * 3;
	int T; cin >> T; while(T--)
	Solve();
	return 0;
}
